<?php

// Controller/Default.php

class Controller_Test extends FLEA_Controller_Action
{

    function actionindex()
    {
	echo 'hello world,too!';
    }
    function actionaddrole()
    {
	$rolesManager =& FLEA::getSingleton('Model_roles');
	/* @var $rolesManager MyRolesManager */
	$role = array('rolename' => 'SYSTEM_ADMIN');
	$rolesManager->create($role);
	$role = array('rolename' => 'POWER_USER');
	$rolesManager->create($role);
	$role = array('rolename' => 'MANAGER');
	$rolesManager->create($role);
     }
    function actionadduser() {
	$usersManager =& FLEA::getSingleton('Model_users');
	/* @var $usersManager MyUsersManager */
	$user = array(
	    'username' => 'dualface',
	    'password' => '12345678',
	    'email' => 'dualface@gmail.com',
	);
	$usersManager->create($user);
	echo 'adduser success!';
	}
    function actionmanager() {
	$usersManager =& FLEA::getSingleton('Model_users');
	/* @var $usersManager MyUsersManager */

	// 取出用户
	$user = $usersManager->findByUsername('zhangsan');
	// 清空现有的角色信息
	$user[$usersManager->rolesField] = array();

	// 取出 POWER_USER 角色
	$rolesManager =& FLEA::getSingleton('Model_roles');
	/* @var rolesManager MyRolesManager */
	$role = $rolesManager->find(array('rolename' => 'POWER_USER'));
	// 指定给用户
	$user[$usersManager->rolesField][] = $role[$rolesManager->primaryKey];

	// 取出 MANAGER 角色
	$role = $rolesManager->find(array('rolename' => 'MANAGER'));
	// 指定给用户
	$user[$usersManager->rolesField][] = $role[$rolesManager->primaryKey];

	// 保存修改后的用户信息
	$usersManager->update($user);

	// 重新从数据库读取用户信息，确定为用户指定的角色信息已经保存到数据库
	$user = $usersManager->findByUsername('dualface');
	dump($user);
	}
}
